package com.song.filter;

import com.song.utils.JwtUtils;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
//@WebFilter(urlPatterns = "/*")
public class TokenFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();

        if(requestURI.contains("/login")){
            log.info("登录请求");
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        String token = request.getHeader("token");
        if(token == null || token.isEmpty()){
            log.info("token为空");
            response.setStatus(401);
            return;
        }

        try {
            JwtUtils.parseToken(token);

        } catch (Exception e){
            log.info("token无效");
            response.setStatus(401);
            return;
        }


        log.info("token有效");
        filterChain.doFilter(servletRequest, servletResponse);
    }


}
